package com.lanou.bookstore.order.dao;

import com.lanou3g.bookstore.book.domain.Book;
import com.lanou3g.bookstore.order.domain.Order;
import com.lanou3g.bookstore.order.domain.OrderItem;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 姚云鹏
 * Create at: 2018/6/1 14:44
 */
public class OrderDao {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        String uid = "1";

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql:///bookstore", "root",
                "root");

        String sql = "SELECT * FROM orders o, orderitem i, book b WHERE o.oid = i.oid AND i.bid =" +
                " b.bid AND o.uid=?";

        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, uid);
        ResultSet resultSet = ps.executeQuery();

        List<Order> orders = new ArrayList<Order>();
        while (resultSet.next()) {
            boolean flag = false;
            for (Order order : orders) {
                if (order.getOid().equals(resultSet.getString("oid"))) {
                    flag = true;

                    Book book = new Book();
                    book.setBid(resultSet.getString("bid"));
                    book.setBname(resultSet.getString("bname"));
                    book.setPrice(resultSet.getDouble("price"));
                    book.setAuthor(resultSet.getString("author"));
                    book.setImage(resultSet.getString("image"));
                    book.setCid(resultSet.getString("cid"));

                    OrderItem orderItem = new OrderItem();
                    orderItem.setIid(resultSet.getString("iid"));
                    orderItem.setCount(resultSet.getInt("count"));
                    orderItem.setSubtotal(resultSet.getDouble("subtotal"));
                    orderItem.setBid(resultSet.getString("bid"));
                    orderItem.setOid(resultSet.getString("oid"));
                    orderItem.setBook(book);

                    order.getOrderItemList().add(orderItem);
                }
            }

            if(!flag){
                Order order = new Order();
                order.setOid(resultSet.getString("oid"));
                order.setOrderTime(resultSet.getString("ordertime"));
                order.setTotal(resultSet.getDouble("total"));
                order.setState(resultSet.getInt("state"));
                order.setUid(resultSet.getString("uid"));
                order.setAddress(resultSet.getString("address"));

                Book book = new Book();
                book.setBid(resultSet.getString("bid"));
                book.setBname(resultSet.getString("bname"));
                book.setPrice(resultSet.getDouble("price"));
                book.setAuthor(resultSet.getString("author"));
                book.setImage(resultSet.getString("image"));
                book.setCid(resultSet.getString("cid"));

                OrderItem orderItem = new OrderItem();
                orderItem.setIid(resultSet.getString("iid"));
                orderItem.setCount(resultSet.getInt("count"));
                orderItem.setSubtotal(resultSet.getDouble("subtotal"));
                orderItem.setBid(resultSet.getString("bid"));
                orderItem.setOid(resultSet.getString("oid"));
                orderItem.setBook(book);

                order.getOrderItemList().add(orderItem);

                orders.add(order);
            }
        }

        System.out.println(orders);

        conn.close();
    }
}
